package cn.nawang.ebeim.api.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;

import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.domain.StorageVersionTreeToDo;
import cn.nawang.ebeim.api.vo.StorageVersionTreeToDoView;

/**
 * @ClassName: StorageVersionTreeToDoQueryRepository
 * @Description: TODO
 * @author fanyb
 * @date Aug 26, 2015 2:28:59 PM
 * @version v1.0
 */
public class StorageVersionTreeToDoRepositoryImpl extends BaseQuery {

  @Autowired
  private EntityManagerFactory entityManagerFactory;

  /**
   * 
   * @author fanyb
   * @Description: 批量删除StorageVersionTreeToDo
   * @param ids 编号
   * @return boolean 删除结果
   * @date Aug 26, 20152:39:15 PM
   *
   * @修改人
   * @修改日期
   */
  public boolean delete(String ids) {
    try {
      String sql = "delete StorageVersionTreeToDo svtd where svtd.id in " + ids;
      Map<String, Object> paraMap = new HashMap<String, Object>();
      boolean deleteFlag = this.execHQL(sql, paraMap);
      return deleteFlag;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找StorageVersionTreeToDo对象列表
   * @param count 查询数目
   * @param status 转态
   * @return List<StorageVersionTreeToDo>
   * @date Aug 26, 20156:22:22 PM
   *
   * @修改人
   * @修改日期
   */
  public List<StorageVersionTreeToDoView> findAll(int count, String status) {
    String sql =
        "select new cn.nawang.ebeim.api.vo.StorageVersionTreeToDoView(sftd.id,sftd.datasourceId,sftd.version,sftd.storageCloudId1,"
            + "sftd.storageCloudId2,sftd.storageCloudId3,sftd.copyCount,sftd.status,ss.customerId,c.maxCopys) "
            + "from StorageVersionTreeToDo sftd,Datasource as ds,StorageStrategy as ss,Customer as c  "
            + "where sftd.datasourceId=ds.id and ss.customerId=ds.customerId and sftd.status=:status "
            + "and c.id=ds.customerId";
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
      Query query = em.createQuery(sql);
      query.setParameter("status", status);
      query.setMaxResults(count);
      List<StorageVersionTreeToDoView> storageVersionTreeToDos = query.getResultList();
      
      return storageVersionTreeToDos;
    } catch (Exception e) {
      e.printStackTrace();
    }finally{
      em.close();
    }
    return null;
  }

  /**
   * 
   * @author fanyb
   * @Description: TODO
   * @param ids
   * @return boolean
   * @date Aug 27, 20154:59:14 PM
   *
   * @修改人
   * @修改日期
   */
  public boolean update(String ids) {
    try {
      String sql = "update StorageVersionTreeToDo svtd set svtd.status='1' where svtd.id in " + ids;
      Map<String, Object> paraMap = new HashMap<String, Object>();
      boolean deleteFlag = this.execHQL(sql, paraMap);
      return deleteFlag;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  public List<StorageVersionTreeToDo> findByProperty(String storageCloudId, String datasourceId,
      String version) {
    List<StorageVersionTreeToDo> objList = null;
    String sql =
        "select sftd from StorageVersionTreeToDo sftd where ( sftd.storageCloudId1=:storageCloudId "
            + "or sftd.storageCloudId2=:storageCloudId or sftd.storageCloudId3=:storageCloudId ) and "
            + "sftd.datasourceId=:datasourceId and sftd.version=:version";
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("storageCloudId", storageCloudId);
      paraMap.put("datasourceId", datasourceId);
      paraMap.put("version", version);
      objList = queryObject(sql, paraMap);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return objList;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据数据源编号和版本号查找StorageVersionTreeToDo对象
   * @param datasourceId
   * @param version
   * @return StorageVersionTreeToDo
   * @date Sep 17, 20159:07:27 AM
   *
   * @修改人
   * @修改日期
   */
  public StorageVersionTreeToDo findByDatasourceIdAndVersion(String datasourceId, String version) {
    List<StorageVersionTreeToDo> objList = null;
    String sql = "select sftd from StorageVersionTreeToDo sftd where "
        + "sftd.datasourceId=:datasourceId and sftd.version=:version";
    try {
      Map<String, Object> paraMap = new HashMap<String, Object>();
      paraMap.put("datasourceId", datasourceId);
      paraMap.put("version", version);
      objList = queryObject(sql, paraMap);
      if (!objList.isEmpty() && objList != null) {
        return objList.get(0);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }
}
